package com.dy.dynotinstalled;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dy.dynotinstalled.room.NotDoorInformation;
import com.dy.dynotinstalled.room.NotDoorInformationViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Li Xuyang
 * @date : 2019/9/26 19:46
 */
public class DoorAdapter extends RecyclerView.Adapter<DoorAdapter.MyViewHolder> {


    private NotDoorInformationViewModel doorInformationViewModel;

    public DoorAdapter(NotDoorInformationViewModel doorInformationViewModel) {
        this.doorInformationViewModel = doorInformationViewModel;
    }

    public void setAllNotDoorInformation(List<NotDoorInformation> allNotDoorInformation) {
        this.allNotDoorInformation = allNotDoorInformation;
    }

    List<NotDoorInformation> allNotDoorInformation = new ArrayList<>();


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;

        itemView = layoutInflater.inflate(R.layout.door_information_card, parent, false);

        final MyViewHolder holder = new MyViewHolder(itemView);

        //长按删除
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //利用tag绑定数据
                final NotDoorInformation notDoorInformation = (NotDoorInformation) holder.itemView.getTag(R.id.doorInformation_for_view_holder);

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(holder.itemView.getContext());
                alertDialog.setTitle("删除本条数据");
                alertDialog.setMessage("是否删除这条数据，删除就不可恢复了");
                alertDialog.setCancelable(true);
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        doorInformationViewModel.deleteDoorInformation(notDoorInformation);
                        Toast.makeText(holder.itemView.getContext(), "删除成功", Toast.LENGTH_SHORT).show();


                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                alertDialog.show();

                return true;
            }
        });

        //正常点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //利用tag绑定数据
                NotDoorInformation notDoorInformation = (NotDoorInformation) holder.itemView.getTag(R.id.doorInformation_for_view_holder);


            }
        });

        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        NotDoorInformation notDoorInformation = allNotDoorInformation.get(position);
        holder.itemView.setTag(R.id.doorInformation_for_view_holder, notDoorInformation);
        holder.textViewNumber.setText(String.valueOf(position + 1));
        holder.textViewCommunity.setText(notDoorInformation.getGlobal_id());
        holder.textViewStreet.setText(notDoorInformation.getType());
        holder.textNumPrincipal.setText(notDoorInformation.getCity());
        holder.isUploadSuccessful.setText(notDoorInformation.getIsUploadSuccessful());
    }


    @Override
    public int getItemCount() {
        return allNotDoorInformation.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView textViewNumber, textViewCommunity, textViewStreet, textNumPrincipal, isUploadSuccessful;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewNumber = itemView.findViewById(R.id.textViewNumber);
            textViewCommunity = itemView.findViewById(R.id.global_id);
            textViewStreet = itemView.findViewById(R.id.textViewStreet);
            textNumPrincipal = itemView.findViewById(R.id.text_num_principal);
            isUploadSuccessful = itemView.findViewById(R.id.isUploadSuccessful);
        }
    }


}
